<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class AdminController extends Controller
{
    protected $middleware = ['App\Http\Middleware\Authenticate'=>[]];
    /*public function artadd(){
    	
    	return view('admin.artadd');
    }
    public function artpost(){
    	return "insert into database";
    }
     public function catadd(){
    	return view('admin.catadd');
    }
     public function catpost(Request $req){
     	$str=str_random();
     	$req->file('pic')->move(public_path().'/images',"$str.jpg");
     	$path='/images/'.$str.'.jpg';
     	$rs=DB::table('cats')->insert(['catname'=>$_POST['catname'],'pic'=>$path]);
    	//return view('admin.catadd');
    	return $rs?'ok':'fail';
    }*/

    public function index(Request $req){ 
        if ($req->user()->admin!=='admin') {
             return redirect('/');  
            }    
        return view('admin.index');
    }

    public function add(Request $req){
        if ($req->user()->admin!=='admin') {
             return redirect('/');  
            }    
        $cat=DB::table('cats')->get();
        return view('admin.add',['cat'=>$cat]);
    }

    public function addpost(Request $req){
        if ($req->user()->admin!=='admin') {
             return redirect('/');  
            }    
        $str=str_random();
        $req->file('pic')->move(public_path().'/images',"$str.jpg");
        $path='/images/'.$str.'.jpg';
        $time=time();
        $rs = DB::table('arts')->insert([
        'title'=>$_POST['title'] ,
        'author'=>$_POST['author'],
        'http'=>$_POST['http'],
        'pic'=>$path,
        'pubtime'=>$time,
        'intro'=>$_POST['intro'],
        'cid'=>$_POST['catname'],
        'content'=>$_POST['content']]);
        return $rs ? 'OK' : 'fail';
    }

    public function lists(Request $req){
        if ($req->user()->admin!=='admin') {
             return redirect('/');  
            }    
        $art=DB::table('arts')->paginate(5);
        return view('admin.lists',['art'=>$art]);
    }

    public function update(Request $req ,$aid){
        if ($req->user()->admin!=='admin') {
             return redirect('/');  
            }    
        $update=DB::table('arts')->where('aid',$aid)->first();
        return view('admin.update',['update'=>$update]);
    }

    public function updatepost(Request $req, $aid){
        if ($req->user()->admin!=='admin') {
             return redirect('/');  
            }    
        $str=str_random();
        $req->file('pic')->move(public_path().'/images',"$str.jpg");
        $path='/images/'.$str.'.jpg';
        $time=time();
        $rs = DB::table('arts')->where('aid',$aid)->update([
        'title'=>$_POST['title'] ,
        'author'=>$_POST['author'],
        'http'=>$_POST['http'],
        'pic'=>$path,
        'pubtime'=>$time,
        'intro'=>$_POST['intro'],
        'status'=>$_POST['status'],
        'content'=>$_POST['content']]);
        return $rs ? 'OK' : 'fail';

    }

    public function del(Request $req, $aid){
        if ($req->user()->admin!=='admin') {
             return redirect('/');  
            }    
        if(DB::table('arts')->where('aid',$aid)->delete()){
            return redirect('admin/lists');
        }else {
            return '删除失败';
        }
    }

    public function pass(Request $req){
        if ($req->user()->admin!=='admin') {
             return redirect('/');  
            }    
        return view('admin.pass');
    }

    public function wxlists(Request $req){
        if ($req->user()->admin!=='admin') {
             return redirect('/');  
            }
        $wx=DB::table('wxusers')->paginate(3);
        return view('admin.wxlists',['wx'=>$wx]);
    }

}
